using Digitalmes.Application.Sys.Menus;
using Digitalmes.Application.Sys.Menus.Commands;
using Digitalmes.Application.Sys.Menus.Queries;

namespace Digitalmes.WebApi.Endpoints.Sys;

/// <summary>
/// 路由终结点。
/// </summary>
public sealed class MenuEndpoint : EndpointGroupBase
{
    public override void Map(WebApplication app)
    {
        app.MapGroup(this, "sys", "menu")
            .RequireAuthorization()
            .MapGet(GetMenuListAsync, "list")
            .MapGet(GetMenuTreeAsync, "tree")
            .MapPost(CreateMenuAsync)
            .MapPut(UpdateMenuAsync, "{id}")
            .MapDelete(DeleteMenuAsync, "{id}");
    }

    [EndpointDescription("查询菜单集合")]
    public async Task<IApiResult> GetMenuListAsync(ISender sender, [AsParameters] GetMenuListQuery query)
    {
        var result = await sender.Send(query);
        return result.ToApiResult();
    }

    [EndpointDescription("按树结构查询菜单")]
    public async Task<IApiResult> GetMenuTreeAsync(ISender sender, [AsParameters] GetMenuListWithTreeQuery query)
    {
        var result = await sender.Send(query);
        return result.ToApiResult();
    }

    [EndpointDescription("创建菜单")]
    public async Task<IApiResult> CreateMenuAsync(ISender sender, SysMenuDto dto)
    {
        var result = await sender.Send(new CreateMenuCommand(dto));
        return result.ToApiResult();
    }

    [EndpointDescription("更新菜单信息")]
    public async Task<IApiResult> UpdateMenuAsync(ISender sender, long id, SysMenuDto dto)
    {
        var result = await sender.Send(new UpdateMenuCommand(id, dto));
        return result.ToApiResult();
    }

    [EndpointDescription("删除菜单")]
    public async Task<IApiResult> DeleteMenuAsync(ISender sender, long id)
    {
        var result = await sender.Send(new DeleteMenuCommand(id));
        return result.ToApiResult();
    }
}
